<?php
namespace Admin\Models\hrm;

use Helpers\Cache;
use Helpers\Main;
use Helpers\Tool;

class XhhrmDepart extends basicModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iDepId;
    /**
     * @var string
     * @Column(type="string", length=100, nullable=true)
     */
    public $sTag;

    /**
     *
     * @var string
     * @Column(type="string", length=100, nullable=true)
     */
    public $sDepName;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iParentId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mDescribe;

    /**
     *
     * @var string
     * @Column(type="string", length=100, nullable=true)
     */
    public $sStatus;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var string
     * @Column(type="string", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     * @Column(type="string", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource("XHHrm_Depart");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHHrm_Depart';
    }


    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhhrmDepart[]|XhhrmDepart
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhhrmDepart
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 缓存部门枚举
     * @return bool|mixed|null
     * created by dale 2017/9/26
     */
    public static function getDepEnum()
    {
        if (!Cache::exists('DepEnum')) {
            $obj = self::find([
                'columns'    => 'iDepId,sDepName',
                'conditions' => 'bDelete=0',
            ]);
            $array = array_column($obj->toArray(), 'sDepName', 'iDepId');
            Cache::save('DepEnum', $array);
            return $array;
        } else {
            return Cache::get('DepEnum');
        }
    }

    /**
     * 根据部门ids字段将ids替换文字
     *
     * @param $sDepIds
     * @param array $depEnum
     * @return string
     * created by dale 2017/9/26
     * modify by dale 20181224
     */
    public static function getDepName($sDepIds, $depEnum = [])
    {
        $ids = array_filter(explode(',', $sDepIds));
        if (!$ids) {
            return '';
        }
        krsort($ids);
        $ids = array_values($ids);
        if (!$depEnum) {
            $depEnum = self::getDepEnum();
        }
        $nameArr = [];
        foreach ($ids as $id) {
            $depName = Tool::getValue($depEnum, $id, '');
            if ($depName) {
                $nameArr[] = $depName;
            }
        }
        $sDepName = implode(':', $nameArr);
        return $sDepName;
    }

    /**
     * 获取部门名称 顶级（例如：技术部，运营部）
     *
     * @param $sDepIds
     *
     * @return mixed|string
     *
     * create by dale  20181214
     */
    public static function getFirstDepName($sDepIds)
    {
        $ids = explode(',', $sDepIds);
        $ids = array_filter($ids);
        if ($ids == []) {
            return '';
        }
        $firstDepId = end($ids);
        return Tool::getValue(self::getDepEnum(), $firstDepId, '');
    }

    /**
     *
     * @param $iParentId
     *
     * @return array
     *
     * create by dale  20180925
     */
    public static function getDeps($iParentId)
    {
        $obj = self::find([
            'columns'     => 'iDepId,sDepName',
            'conditions'  => 'bDelete=0 AND iParentId = :iParentId:',
            'bind'        => ['iParentId'=>$iParentId],
        ]);
        return array_column($obj->toArray(), 'sDepName', 'iDepId');
    }

    /**
     * 获取部门id
     *
     * @param integer $nowId  获取当前部门
     * @param array $acceptArr 部门id连数组
     *
     * @return array
     *
     * create by dale  20181008
     */
    public static function getDepIdArr($nowId, $acceptArr = [])
    {
        $nowDep = XhhrmDepart::findFirst([
            'conditions' => 'bDelete=0 AND iDepId=:iDepId:',
            'bind' => ['iDepId' => $nowId]
        ]);
        $acceptArr[] = $nowDep->iDepId;
        if ($nowDep->iParentId == 0) {
            return $acceptArr;
        } else {
            return self::getDepIdArr($nowDep->iParentId, $acceptArr);
        }
    }

    /**
     * 根据部门id字段获取部门名称 ：分割
     *
     * @param $obj
     * @param $fields
     * @param bool $isArray
     *
     * create by dale  20181224
     */
    public static function runDepNameByDepId(&$obj, $fields, $isArray = true)
    {
        $depEum = self::getDepEnum();
        is_string($fields) and $fields = [$fields => $fields];
        if ($isArray) {
            foreach ($obj as $index => $objOne) {
                foreach ($fields as $idField => $nameField) {
                    $depIds = Tool::getValue($objOne, $idField);
                    if (!$depIds) {
                        continue;
                    }
                    $obj[$index][$nameField] = XhhrmDepart::getDepName($depIds, $depEum);
                }
            }
        } else {
            foreach ($fields as $idField => $nameField) {
                $depIds = Tool::getValue($obj, $idField);
                if (!$depIds) {
                    continue;
                }
                $obj[$nameField] = XhhrmDepart::getDepName($depIds, $depEum);
            }
        }
    }

}
